import java.util.*;

/**
 * 桶排序
 * 找出数组中出现次数最多的前k个元素
 */
public class BucketSort {
    public List BucketSort(int[] nums,int k){
        int count = nums.length;
        int[] newnums = new int[count];
        Map<Integer,Integer> map =new HashMap<>();
        for(int num:nums){
            if(map.containsKey(num)) {
                map.put(num, map.get(num)+1);
            } else{
                map.put(num,1);
            }
        }
        List<Map.Entry<Integer, Integer>> list=new ArrayList(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                return o2.getValue()-o1.getValue();  //降序
            }
        });
        System.out.println("list:"+list);
        List newlist = new LinkedList();
        for(Map.Entry<Integer,Integer> entry:list){
            newlist.add(entry.getKey());
            if(newlist.size()==k){
                System.out.println(newlist);
                break;
            }
        }
        return newlist;
    }

    public static void main(String[] args){
        int[] nums={1,1,1,2,2,3};
        int key=2;
        System.out.println(new BucketSort().BucketSort(nums,key));
    }
}
